/**============================================================================
 Confidential Information - Limited distribution to authorized persons only.
 This software is protected as an unpublished work under the U.S. Copyright
 Act of 1976.

 Copyright (c) 2011, BluePhoenix Solutions, Inc. All rights reserved.
 ----------------------------------------------------------------------------
 file......: NotepadPanel.java
 Author....: Vyacheslav Kovalyov
 Created...: 23.10.11

 Description:

 ============================================================================*/
package ru.monyag.pred.swing.ui.notepad;

import ru.monyag.pred.core.notepad.Note;
import ru.monyag.pred.swing.ui.listeners.IUpdateListener;
import ru.monyag.pred.swing.ui.table.BaseTable;

import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import static ru.monyag.pred.swing.util.PredSwingConstants.DOUBLE_CLICK;

/**
 * @author Vyacheslav Kovalyov
 * @version 1.0
 */
public class NotepadTablePane extends JScrollPane implements IUpdateListener<Note> {

    public NotepadTablePane() {

        ScrollPaneLayout layout = new ScrollPaneLayout();
        layout.setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_AS_NEEDED);
        setLayout(layout);

        table = new BaseTable<Note>(new NotepadTableModel());
        table.setShowGrid(true);
        table.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == DOUBLE_CLICK) {
                    new NotepadEditorWindow(table.getSelectedRowObject(), NotepadTablePane.this).setVisible(true);
                }
            }
        });

        setViewportView(table);
        setData();
    }

    @Override
    public void dispatchUpdate(Note object) {
        table.updateRow(object);
    }

    @Deprecated //test staff
    private void setData() {
        Note note1 = new Note();
        note1.setDate("note1.setDate");
        note1.setMessage("note1.setMessage");
        note1.setTags("note1.setTags");
        note1.setTitle("note1.setTitle");


        Note note2 = new Note();
        note2.setDate("note2.setDate");
        note2.setMessage("note2.setMessage");
        note2.setTags("note2.setTags");
        note2.setTitle("note2.setTitle");


        Note note3 = new Note();
        note3.setDate("note3.setDate");
        note3.setMessage("note3.setMessage");
        note3.setTags("note3.setTags");
        note3.setTitle("note3.setTitle");

        ArrayList<Note> list = new ArrayList<Note>();
        list.add(note1);
        list.add(note2);
        list.add(note3);
        table.setData(list);
    }

    private final BaseTable<Note> table;
}
